from flask import request, jsonify
from server.nurse import patient_list_server
from api import patient_list_api

@patient_list_api.route('/read', methods=['GET'])
def read():
    result = patient_list_server.list_read()
    return result

@patient_list_api.route('/delete', methods=['POST'])
def delead():
    data = request.json
    result = patient_list_server.id_delete(data)
    return result

@patient_list_api.route('/add', methods=['POST'])
def add():
    data = request.json
    if data["inhospital_time"] == "NaN-NaN-NaN NaN:NaN:NaN":
        print("时间数据为 'NaN-NaN-NaN NaN:NaN:NaN'")
        data["inhospital_time"]="0-0-0 0:0:0"
    if data["outhospital_time"] == "NaN-NaN-NaN NaN:NaN:NaN":
        print("时间数据为 'NaN-NaN-NaN NaN:NaN:NaN'")
        data["outhospital_time"]="0-0-0 0:0:0"
    if data["inhospital_time"] == "1970-01-01 08:00:00":
        print("时间数据为 'NaN-NaN-NaN NaN:NaN:NaN'")
        data["inhospital_time"]="0-0-0 0:0:0"
    if data["outhospital_time"] == "1970-01-01 08:00:00":
        print("时间数据为 'NaN-NaN-NaN NaN:NaN:NaN'")
        data["outhospital_time"]="0-0-0 0:0:0"
    result = patient_list_server.add(data)
    print(data)

    return result

@patient_list_api.route('/update', methods=['POST'])
def update():
    data = request.json
    if data["inhospital_time"] == "NaN-NaN-NaN NaN:NaN:NaN":
        print("时间数据为 'NaN-NaN-NaN NaN:NaN:NaN'")
        data["inhospital_time"]="0-0-0 0:0:0"
    if data["outhospital_time"] == "NaN-NaN-NaN NaN:NaN:NaN":
        print("时间数据为 'NaN-NaN-NaN NaN:NaN:NaN'")
        data["outhospital_time"]="0-0-0 0:0:0"
    if data["inhospital_time"] == "1970-01-01 08:00:00":
        print("时间数据为 'NaN-NaN-NaN NaN:NaN:NaN'")
        data["inhospital_time"]="0-0-0 0:0:0"
    if data["outhospital_time"] == "1970-01-01 08:00:00":
        print("时间数据为 'NaN-NaN-NaN NaN:NaN:NaN'")
        data["outhospital_time"]="0-0-0 0:0:0"
    result = patient_list_server.update(data)
    return result

@patient_list_api.route('/importing', methods=['POST'])
def importing():
    # 检查请求中是否包含文件
    if 'file' not in request.files:
        return jsonify({"error": "No file part"}), 400

    file = request.files['file']
    print(f"file的文件是 = {file}")

    result = patient_list_server.importing(file)
    return result
